package com.neusoft.nep.NEPS.controller;

import com.neusoft.nep.entity.AirQualityLevel;
import com.neusoft.tools.*;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.input.MouseButton;
import javafx.scene.text.Text;

import java.io.IOException;

public class FeedbackController {

    private ToggleGroup toggleGroup;
    @FXML
    private RadioButton leverOne;
    @FXML
    private RadioButton leverTwo;
    @FXML
    private RadioButton leverThree;
    @FXML
    private RadioButton leverFour;
    @FXML
    private RadioButton leverFive;
    @FXML
    private RadioButton leverSix;
    @FXML
    private TableView<AirQualityLevel> qualityTable;
    @FXML
    private TableColumn<AirQualityLevel, String> descriptionColumn;
    @FXML
    private TextArea feedbackTextArea;
    @FXML
    private Button submitButton;

    @FXML
    public void initialize() {
        //  填入表格中的数据
        ObservableList<AirQualityLevel> data = FXCollections.observableArrayList(
                new AirQualityLevel("一", "优", "空气质量令人满意，基本无空气污染", "各类人群可正常活动"),
                new AirQualityLevel("二", "良", "空气质量可接受，但某些污染物可能对极少数异常敏感人群健康有较弱影响", "极少数异常敏感人群应减少户外活动"),
                new AirQualityLevel("三", "轻度污染", "易感人群症状有轻度加剧，健康人群出现刺激症状", "儿童、老年人及心脏病、呼吸系统疾病患者应减少长时间、高强度的户外锻炼"),
                new AirQualityLevel("四", "中度污染", "进一步加剧易感人群症状，可能对健康人群心脏、呼吸系统有影响", "儿童、老年人及心脏病、呼吸系统疾病患者避免长时间、高强度的户外锻炼，一般人群适量减少户外运动"),
                new AirQualityLevel("五", "重度污染", "心脏病和肺病患者症状显著加剧，运动耐受力降低，健康人群普遍出现症状", "儿童、老年人及心脏病、肺病患者应停留在室内，停止户外运动，一般人群减少户外运动"),
                new AirQualityLevel("六", "严重污染", "健康人群运动耐受力降低，有明显强烈症状，提早出现某些疾病", "儿童、老年人和病人应当留在室内，避免体力消耗，一般人群应避免户外活动")
        );
        toggleGroup = new ToggleGroup();
        leverOne.setToggleGroup(toggleGroup);
        leverTwo.setToggleGroup(toggleGroup);
        leverThree.setToggleGroup(toggleGroup);
        leverFour.setToggleGroup(toggleGroup);
        leverFive.setToggleGroup(toggleGroup);
        leverSix.setToggleGroup(toggleGroup);

        //  实现自动换行
        descriptionColumn.setCellFactory(column -> new TableCell<>() {
            @Override
            protected void updateItem(String item, boolean empty) {
                super.updateItem(item, empty);
                //  如果单元格是空的就清除这一单元格
                if (item == null || empty) {
                    setText(null);
                    setGraphic(null);
                } else {
                    Text text = new Text(item);
                    //  让描述列的宽度与窗口的宽度绑定
                    text.wrappingWidthProperty().bind(column.widthProperty().subtract(10));
                    setGraphic(text);
                    setText(null);
                }
            }
        });
        //  当捕捉到鼠标双击某一AQI的时候，弹出一个有关这一AQI详情的弹窗
        qualityTable.setOnMouseClicked(event -> {
            if (event.getButton().equals(MouseButton.PRIMARY) && event.getClickCount() == 2) {

                AirQualityLevel selectedLevel = qualityTable.getSelectionModel().getSelectedItem();
                if (selectedLevel != null) {
                    AlertUtils.showAirQualityDetails(selectedLevel);
                }
            }
        });


        qualityTable.setItems(data);

        qualityTable.setFixedCellSize(45);
        qualityTable.prefHeightProperty().bind(
                qualityTable.fixedCellSizeProperty().multiply(qualityTable.getItems().size()).add(40)
        );
    }

    /**
     *  点击提交后，将AQI反馈信息保存，并跳转到用户个人的反馈日志界面
     */
    public void submit() throws IOException {
        //  检测表单是否全部已经填写，随后将数据读入FeedbackSession，之后跳转到用户的反馈日志页面
        String feedbackText = feedbackTextArea.getText();
        RadioButton selectedRadio = (RadioButton) toggleGroup.getSelectedToggle();
        if (selectedRadio == null) {
            AlertUtils.showDialog("警告", "请选择预估的空气质量指数等级。");
            return;
        }
        String AQI = selectedRadio.getText();
        if(ValidationUtils.isNullOrBlank(feedbackText)){
            AlertUtils.showDialog("警告", "请填写反馈信息。");
        }else{
            FeedbackSession.getInstance().setAQI(AQI, feedbackText);
            AlertUtils.showDialog(null, "反馈成功!");
            //  向文件中写入新的反馈
            FeedbackUtils.addFeedback(FeedbackSession.getInstance().getFeedback().getUsername(),
                    FeedbackSession.getInstance().getFeedback().getPhoneNumber(), FeedbackSession.getInstance().getFeedback().getProvince(),
                    FeedbackSession.getInstance().getFeedback().getCity(), FeedbackSession.getInstance().getFeedback().getAddress(),
                    FeedbackSession.getInstance().getFeedback().getLevel(), FeedbackSession.getInstance().getFeedback().getContent());
            FXMLUtils.switchToShow(submitButton, "NEPS/view/LogView.fxml");
        }
    }
}